package com.stylefeng.guns.modular.system.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.stylefeng.guns.common.constant.Const;
import com.stylefeng.guns.common.persistence.dao.DeptMapper;
import com.stylefeng.guns.common.persistence.dao.UserMapper;
import com.stylefeng.guns.common.persistence.model.Dept;
import com.stylefeng.guns.common.persistence.model.User;
import com.stylefeng.guns.core.base.tips.ErrorTip;
import com.stylefeng.guns.core.base.tips.SuccessTip;
import com.stylefeng.guns.core.base.tips.Tip;
import com.stylefeng.guns.core.shiro.ShiroKit;
import com.stylefeng.guns.modular.system.service.IDeptService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Service
@Transactional
public class DeptServiceImpl implements IDeptService {

    @Resource
    DeptMapper deptMapper;
    @Resource
    UserMapper userMapper;

    @Override
    public void deleteDept(Integer deptId) {

        Dept dept = deptMapper.selectById(deptId);

        Wrapper<Dept> wrapper = new EntityWrapper<>();
        wrapper = wrapper.like("pids", "%[" + dept.getId() + "]%");
        List<Dept> subDepts = deptMapper.selectList(wrapper);
        for (Dept temp : subDepts) {
            temp.deleteById();
        }

        dept.deleteById();
    }

    @Override
    public Tip insertDept(Dept dept, User user) {
        //账号是否重复
        Wrapper<User> wrapper = new EntityWrapper<>();
        wrapper = wrapper.where("account={0}", user.getAccount());
        int i=userMapper.selectCount(wrapper);
        if(i>0){
            return new ErrorTip(Const.FAILUER,"登陆账号已存在，请重新填写");
        }
        //商户全称
        Wrapper<Dept> wrapper_dept = new EntityWrapper<>();
        wrapper_dept = wrapper_dept.where("fullname={0}", dept.getFullname());
        int k=deptMapper.selectCount(wrapper_dept);
        if(k>0){
            return new ErrorTip(Const.FAILUER,"商户全称已存在，请重新填写");
        }
        if(deptMapper.insert(dept)>0){
            user.setUserType("0");//默认企业管理员
            user.setDeptid(dept.getId());
            user.setName(dept.getSimplename());
            user.setSex(1);//默认男性
            user.setStatus(1);//状态(1：启用  2：冻结  3：删除）
            user.setRoleid(Const.COMPANY_ROLE);//默认商户管理员角色
            user.setSalt(ShiroKit.getRandomSalt(5));
            user.setPassword(ShiroKit.md5(user.getPassword(), user.getSalt()));
            if(userMapper.insert(user)>0){
                return new SuccessTip();
            }else{
                return new ErrorTip(Const.FAILUER,"新增账号失败");
            }
        }else{
            return new ErrorTip(Const.FAILUER,"新增商户失败");
        }

    }

    @Override
    public Tip updateDept(Dept dept) {
        //商户全称
        Wrapper<Dept> wrapper_dept = new EntityWrapper<>();
        wrapper_dept = wrapper_dept.where("fullname={0}", dept.getFullname()).andNew("id !={0}",dept.getId());
        int k=deptMapper.selectCount(wrapper_dept);
        if(k>0){
            return new ErrorTip(Const.FAILUER,"商户全称已存在，请重新填写");
        }
        if(deptMapper.updateById(dept)>0){
           return new SuccessTip();
        }else{
            return new ErrorTip(Const.FAILUER,"修改商户失败");
        }
    }
}
